package com.wx.activitys.mayday.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.github.sd4324530.fastweixin.api.JsAPI;
import com.github.sd4324530.fastweixin.api.response.GetSignatureResponse;
import com.github.sd4324530.fastweixin.api.response.GetUserInfoResponse;
import com.wx.activitys.base.controller.BaseController;
import com.wx.activitys.mayday.model.MaydayArea;
import com.wx.activitys.mayday.model.MaydayTeam;
import com.wx.activitys.mayday.model.MaydayTeamMember;
import com.wx.activitys.mayday.service.MaydayService;
import com.wx.activitys.valsday.service.ValsDayAccessService;
@Controller
@RequestMapping("/mayday")
public class MaydayController extends BaseController{
	public static final String ROOT_NAME="mayday";
	public static final String BASE_PATH="http://wxh5.xuanzhen-tech.com";
	//public static final String BASE_PATH="http://localhost:8080/wx_activity";
	public static final String BASE_URL=BASE_PATH+"/"+ROOT_NAME;
	@Autowired
	private JsAPI jsAPI;
	@Autowired
	MaydayService maydayService;
	
	@RequestMapping(value = "/map.html",method = RequestMethod.GET)
	public String getMap(Model model, HttpServletRequest request){
		getSign(model,"map.html");
		return "mayday/map";
	}
	
	//点击地图时 创建新团队 新增该地图清洁数量
	@RequestMapping(value = "/area.html",method = RequestMethod.GET)
	public String getArea(Model model, String areaId, HttpServletRequest request){
		//更新
		//String openId = "openId";
		//String name = "name";
		try {
			String teamId = null;
			String openId = ((GetUserInfoResponse)request.getSession().getAttribute("userInfo")).getOpenid();
			String name = ((GetUserInfoResponse)request.getSession().getAttribute("userInfo")).getNickname();
			logger.info("openId is "+openId);
			logger.info("userName is "+name);
			try {
				teamId = maydayService.updateArea( areaId, openId, name);
				logger.info("teamId is "+teamId);
			} catch (Exception e) {
				e.printStackTrace();
			}
			//teamId返回error时直接跳出
			if (teamId==null || "error".equals(teamId)) {
				System.out.println("error");
				return "error/error-500";
			}
			//teamId正确时
			model.addAttribute("teamId", teamId);
			model.addAttribute("areaId", areaId);
			getSign(model,"area.html?areaId="+areaId);
			System.out.println(areaId);
			System.out.println("right");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "mayday/clear";
	}
	
	//结束页控制器
	@RequestMapping(value="/end.html", method = RequestMethod.GET)
	public String getEnd(Model model ,String teamId, HttpServletRequest request){
		try {
			//获取当前参与协助的团队
			MaydayTeam team = maydayService.selectTeamByTeamId(teamId);
			logger.info("enter /end.html");
			//投票地区
			model.addAttribute("area", team.getAreaId());
			model.addAttribute("areaName", maydayService.selectArea(team.getAreaId()).getName());
			//队伍信息
			model.addAttribute("team", team);
			//所有的地区的比率
			List<MaydayArea> list = maydayService.selectPercentAreas();
			for (int i = 0; i < list.size(); i++) {
				int comSize = list.get(i).getComSize();
				int size = list.get(i).getSize();
				Double percent = ((double)comSize/size)*100;
				Integer perc = percent.intValue();
				list.get(i).setSize(perc);//百分比填进Size
			}
			//地区的列表
			model.addAttribute("areaList", list);
			
			//根据队伍id获取成员
			List<MaydayTeamMember> memberList = maydayService.selectTeamMember(teamId);
			model.addAttribute("memberList", memberList);
			String openId = ((GetUserInfoResponse)request.getSession().getAttribute("userInfo")).getOpenid();
			//String openId = "openId";
			getSign(model,"end.html?teamId="+teamId);
			if (openId == null ) {
				return "error/error-500";
			}else if (!openId.equals(team.getCreateOpenId())) {
				return "mayday/share_end";
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "mayday/end";
	}
	
	//分享页
	@RequestMapping(value="/share.html", method = RequestMethod.GET)
	public String getShare(Model model, String teamId, HttpServletRequest request){
		//获取个人信息
		MaydayTeam team = maydayService.selectTeamByTeamId(teamId);
		model.addAttribute("team", team);
		
		//查询地区
		MaydayArea area = maydayService.selectArea(team.getAreaId());
		int comSize = area.getComSize();
		int size = area.getSize();
		Double percent = ((double)comSize/size)*100;
		Integer perc = percent.intValue();
		area.setSize(perc);//百分比填进size
		model.addAttribute("area", area);
		getSign(model,"share.html?teamId="+teamId);
		return "mayday/share";
	}
	
	//协助好友
	@RequestMapping(value="/help.html", method = RequestMethod.GET)
	public String getHelper(Model model, String teamId, HttpServletRequest request){
		//String openId = "helper";
		//String img = "sdad";
		String openId = ((GetUserInfoResponse)request.getSession().getAttribute("userInfo")).getOpenid();
		String img = ((GetUserInfoResponse)request.getSession().getAttribute("userInfo")).getHeadimgurl();
		//查询打扫区域
		MaydayTeamMember member = maydayService.selectTeamMemberByKey(teamId, openId);
		if ( member != null && !member.equals("") ) {
			model.addAttribute("teamId", teamId);
			return "redirect:end.html";
		}
		String areaId = null;
		try {
			//协助打扫 进行新增团队成员和增加打扫次数的操作
			//操作同时返回清理地区
			areaId = maydayService.addTeamParter(teamId, openId, img);
		} catch (Exception e) {
			e.printStackTrace();
		}
		//teamId返回error时直接跳出
		model.addAttribute("teamId", teamId);
		if (areaId==null) {
			System.out.println("error");
			return "error/error-500";
		}else if("error".equals(areaId)){
			//为创建者
			return "redirect:end.html";
		}
		//teamId正确时
		model.addAttribute("areaId", areaId);
		System.out.println("right");
		getSign(model,"help.html?teamId="+teamId);
		return "mayday/clear";
	}
	
	private void getSign(Model model, String url){
		String currentPage=BASE_URL+"/"+url;
		GetSignatureResponse signObj=jsAPI.getSignature(currentPage);
		model.addAttribute("basePath", BASE_PATH);
		model.addAttribute("signObj", signObj);
		model.addAttribute("baseUrl", BASE_URL);
	}
}
